package org.mall.modules.shop.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.mall.modules.shop.entity.ShopCategory;
import org.mall.modules.shop.mapper.ShopCategoryMapper;
import org.mall.modules.shop.mapper.ShopCategoryRelationshipMapper;
import org.mall.modules.shop.service.IShopCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 商品条目管理
 * @Author: jeecg-boot
 * @Date:   2019-11-14
 * @Version: V1.0
 */
@Service
public class ShopCategoryServiceImpl extends ServiceImpl<ShopCategoryMapper, ShopCategory> implements IShopCategoryService {

    @Resource
    private ShopCategoryMapper categoryMapper;
    @Resource
    private ShopCategoryRelationshipMapper relationshipMapper;

    /**
     * 删除商品条目
     * @param id
     */
    @Override
    public void deleteById(String id) {
        categoryMapper.removeById(id);
    }

    /**
     * 批量删除商品条目
     * @param ids
     */
    @Override
    public void deleteByIds(List<String> ids) {
        categoryMapper.removeByIds(ids);
        //删除条目关系表数据
        categoryMapper.removeCategoryRelationshipByCategory(ids);
    }

    @Override
    public void deleteCategoryRelationshipById(String id) {
        if(id != null && StringUtils.isNotEmpty(id)){
            String[] ids = id.split(",");
            categoryMapper.removeCategoryRelationshipByCategory(Arrays.asList(ids));
        }
    }

    /**
     * 查询商品条目下包含的商品信息(上架的，未删除的，通过审核的)
     * @param category
     * @return
     */
    @Override
    public List<Long> querySpuIdsByCategoryIdAndShopId(ShopCategory category) {

        //TODO
        //根据条目ID和商铺ID去商铺条目关联关系表中查询该条目下的所有商品ID

        return categoryMapper.querySpuIdsByCategoryIdAndShopId(category);
    }
    /**
     * 根据shopID查询排序最大值
     * @return
     */
    @Override
    public ShopCategory queryMaxForCategoryRank(String shopId) {
        return categoryMapper.queryMaxForCategoryRank(shopId);
    }
    /**
     * 根据店铺ID，条目名称查询条目
     * @param shopId
     * @return
     */
    @Override
    public int queryCatogeryName(String categoryName ,String shopId) {
        return categoryMapper.queryCatogeryName(categoryName,shopId);
    }

    @Override
    public IPage<Long> pageListOfCategory(Page page, String categoryId) {
        return relationshipMapper.pageListOfCategory(page,categoryId);
    }

    @Override
    public ShopCategory getOneShopCategoryBySpuId(Long shopId) {
        return categoryMapper.getOneShopCategoryBySpuId(shopId);
    }

}
